package com.flycent.fanbao.ui;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.flycent.fanbao.App;
import com.flycent.fanbao.R;
import com.flycent.fanbao.adapter.AbsDoubleViewHolderAdapter;
import com.flycent.fanbao.adapter.ListToGridAdapter;

public class ListofGridActivity extends BackableTitleBarActivity{

	private ListToGridAdapter mAdapter;
	ListView mList;
	View convertView;

	ArrayList<String> mLists;
	private ListAdapter mAdapter2;

	@Override
	protected void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.activity_list_of_grid);
		getTitleBar().setTitle("List实现Grid");

		mLists =  new ArrayList<String>();
		for (int i = 0; i < 30; i++) {
			mLists.add(i+"");
		}
		
		convertView =  (View) LayoutInflater.from(ListofGridActivity.this).inflate(R.layout.activity_text, null);


		mList = (ListView) findViewById(R.id.list_of_grid);

		mList.addHeaderView(convertView);//,null,false

		mAdapter2 = new ListAdapter(this, R.layout.item_list_to_grid);
		mList.setAdapter(mAdapter2);
		
		mAdapter2.update(mLists);
		
//		mAdapter = new ListToGridAdapter(ListofGridActivity.this);
//		mAdapter.setData(mLists);
//		mList.setAdapter(mAdapter);
//		mAdapter.notifyDataSetChanged();


	}

	public class ListAdapter extends AbsDoubleViewHolderAdapter<String>{

		public ListAdapter(Context context, int layoutRes) {
			super(context, layoutRes);
		}

		@Override
		protected void bindData(int pos, String itemData) {
			Button but1 = getViewFromHolder(R.id.item_iamge1);
			Button but2 = getViewFromHolder(R.id.item_iamge2);


			
			//第一列
			final String string1 = mLists.get(pos * 2);
			but1.setText(string1);

			but1.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View paramView) {
					App.showShortToast(string1+"");
				}
			});

			if ((pos * 2 + 1) < mLists.size()) {
				final String string2 = mLists.get(pos * 2 + 1);
				but2.setText(string2);
				but2.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						App.showShortToast(string2+"");
					}
				});
			} else {
				but2.setVisibility(View.INVISIBLE);
			}
		}

	}


}
